import { Message } from '@arco-design/web-vue'
import { usePermission } from '@/hooks/usePermission'
import { setQueryParams } from '@/utils'

// 路由别名配置
const routeAliasConfig = {
  // 资产-车辆管理
  carManage: { label: '车辆管理', permission: 'svms:asstes:car:car-manage', path: '/assets/car/car-manage' },
  // 财务-违章费用
  costViolation: { label: '违章费用', permission: 'svms:finance:costConfirm:violation', path: '/finance/cost-confirm/violation' },
  // 财务-调车结算
  costTransfer: {
    label: '调车结算',
    permission: 'svms:finance:reconciliation:transferReconciliation',
    path: '/finance/reconciliation/transferReconciliation',
  },
  // 财务-短租对账
  sOrderReconciliation: { label: '短租对账', permission: 'svms:finance:examine:short-order', path: '/finance/examine/shortOrder' },
  // 业务-运营成本
  operateCost: { label: '运营成本', permission: 'svms:business:operate-cost', path: '/business/operate-cost' },
  // 财务-运维结算
  maintenanceCost: {
    label: '运维结算',
    permission: 'svms:finance:reconciliation:maintenance-fee',
    path: '/finance/reconciliation/maintenance-fee',
  },
  // 资产-车辆保险
  carInsure: { label: '车辆保险', permission: 'svms:asstes:operation:car-insure', path: '/assets/operation/car-insure' },
  // 财务-车辆购置费结算
  carPurchase: {
    label: '车辆购置费结算',
    permission: 'svms:finance:reconciliation:car-purchase',
    path: '/finance/reconciliation/car-purchase',
  },
}
/**
 * 根据code 和 extraData、queryParam 生成跳转配置
 * @param code code码
 * @param extraData 额外数据
 * @param queryParam 查询参数
 */
const getNavConfig = (code: string, extraData: any, queryParam: any) => {
  let option: any = {}
  let param: any = {
    // 门店列表
    siteIdList: queryParam.siteIdList,
    // 时间筛选
    orderTimeQueryType: queryParam.orderTimeQueryType,
    startTime: queryParam.startTime,
    endTime: queryParam.endTime,
  }
  // 车辆管理
  if (code == 'carManage') {
    option = { ...routeAliasConfig['carManage'] }
    const { siteId, name } = extraData
    // extraData 传入了门店id，说明是点的单独门店
    if (siteId) {
      param = {
        siteIdList: [siteId],
        operateStatusList: [10, 20, 30, 60], // 运营状态：空闲可租、租赁中、运维中、已借出
      }
    } else {
      param = {
        siteIdList: queryParam.siteIdList, // 门店列表
        operateStatusList: [10, 20, 30, 60], // 运营状态：空闲可租、租赁中、运维中
      }
      // 车辆来源
      if (name == '自有') param.sourceIdList = [1] // 自有
      if (name == '借入' || name == '借调（借入）') param.sourceIdList = [2] // 借入
      if (name == '融资租赁') param.sourceIdList = [3] // 融资租赁
      if (name == '挂靠/托管') param.sourceIdList = [4] // 挂靠托管
    }
  }
  // 违章费用
  if (code == 'costViolation') {
    option = { ...routeAliasConfig['costViolation'] }
    param.feeStatuses = [0, 3, 5, 1, 2]
  }
  // 调车结算
  if (code == 'costTransfer') {
    option = { ...routeAliasConfig['costTransfer'] }
    param.typeList = [2]
    param.statusList = [0, 1, 2]
  }
  // 总收入-订单费项统计
  if (code == 'orderFee') {
    option = { ...routeAliasConfig['sOrderReconciliation'] }
    // 结算状态：待结算、部分结算、结算中、已结算
    param.feeStatusList = [0, 1, 2, 3]
    // 订单状态：待排车、待用车、用车中、已还车、已完成、已取消
    param.orderStatusList = [10, 100, 200, 300, 1000, -100]
    // 区分不同费项
    switch (extraData.type) {
      // 订单租金
      case 5:
        param.itemCategoryType = 1
        break
      // 其他费用
      case 6:
        param.itemCategoryType = 2
        break
      // 线上费用
      case 7:
        param.feeType = 2
        break
      // 线下费用
      case 4:
        param.feeType = 1
        break
      // 保险、车损、油电费需要借助传入的参数extendParams
      case 1:
        const tempInsureList = ['基础保险', '不计免赔', '保险升级', '退款-基础保险', '退款-保险升级', '续租-基础保险', '退款-不计免赔']
        if (extraData.childrenList && extraData.childrenList.length > 0) {
          extraData.childrenList.forEach((item: any) => {
            if (item.extendParams && item.extendParams.length > 0) {
              item.extendParams.forEach((feeName: string) => {
                tempInsureList.push(feeName)
                tempInsureList.push('退款-' + feeName)
              })
            }
          })
        }
        param.itemNameList = tempInsureList
        break
      case 2:
        const tempCarDamageList = ['车损费', '退款-车损费']
        if (extraData.extendParams && extraData.extendParams.length > 0) {
          extraData.extendParams.forEach((feeName: string) => {
            tempCarDamageList.push(feeName)
            tempCarDamageList.push('退款-' + feeName)
          })
        }
        param.itemNameList = tempCarDamageList
        break
      case 3:
        const oilElectricityList = ['油（电）费', '退款-油（电）费']
        if (extraData.extendParams && extraData.extendParams.length > 0) {
          extraData.extendParams.forEach((feeName: string) => {
            oilElectricityList.push(feeName)
            oilElectricityList.push('退款-' + feeName)
          })
        }
        param.itemNameList = oilElectricityList
        break
      // 续租费
      case 8:
        param.itemNameList = ['续租-基础保险', '续租-手续费', '续租-车辆租金']
        break
    }
  }
  // 订单渠道统计
  if (code == 'orderChannel') {
    option = { ...routeAliasConfig['sOrderReconciliation'] }
    // 结算状态：待结算、部分结算、结算中、已结算
    param.feeStatusList = [0, 1, 2, 3]
    // 订单状态：待排车、待用车、用车中、已还车、已完成、已取消
    param.orderStatusList = [10, 100, 200, 300, 1000, -100]
    // 点击的渠道类型
    param.channelTypes = [extraData.type]
  }
  // 取车门店统计
  if (code == 'orderGiveCar') {
    option = { ...routeAliasConfig['sOrderReconciliation'] }
    // 点击的取车门店
    param.siteIdList = [extraData.id]
    // 结算状态：待结算、部分结算、结算中、已结算
    param.feeStatusList = [0, 1, 2, 3]
    // 订单状态：待排车、待用车、用车中、已还车、已完成、已取消
    param.orderStatusList = [10, 100, 200, 300, 1000, -100]
  }
  // 订单财务统计
  if (code == 'orderBill') {
    option = { ...routeAliasConfig['sOrderReconciliation'] }
    // 区分不同费项
    switch (extraData.name) {
      case '已结算':
        param.feeStatusList = [2, 3]
        break
      case '待结算':
        param.feeStatusList = [0, 1, 3]
        break
      case '已对账':
        param.billStatusList = [2, 3]
        break
      case '未对账':
        param.billStatusList = [1, 2]
        break
    }
  }
  // 总支出
  if (code == 'totalCost') {
    const { data, childData, isPaid, isWaitPaid } = extraData
    const name = data.name
    const childName = childData?.name
    // 费用类型
    const type = childData ? childData.type : data.type
    param.siteIdList = queryParam.orgSiteIdList
    // 枚举值：
    // 1-运营成本
    // 2-报销单费用
    // 3-付款单费用
    // 4-车辆购置费
    // 5-购车其他费
    // 6-车辆借入
    // 7-运维费
    // 8-保险费
    // 9-保养费
    // 10-维修费
    // 11-年检费
    if (name == '运营成本' || type == 1) {
      option = { ...routeAliasConfig['operateCost'] }
      param.siteIdList = undefined
      param.orgIdList = queryParam.orgIdList
      // 点击的是一级的金额
      if (!childName) {
        // 审批状态
        param.checkStatusList = [1, 2, 3, 4]
        // 付款状态
        param.payStatusList = [0, 1]
      } else {
        if (childName == '报销单费用' || type == 2) {
          // 支出单类型：报销单
          param.costType = 1
          if (isPaid) {
            // 付款状态
            param.payStatusList = [1]
          } else if (isWaitPaid) {
            // 付款状态
            param.payStatusList = [0]
          } else {
            // 审批状态
            param.checkStatusList = [1, 2, 3, 4]
            // 付款状态
            param.payStatusList = [0, 1]
          }
        }
        if (childName == '付款单费用' || type == 3) {
          // 支出单类型：付款单
          param.costType = 2
          if (isPaid) {
            // 付款状态
            param.payStatusList = [1]
          } else if (isWaitPaid) {
            // 付款状态
            param.payStatusList = [0]
          } else {
            // 审批状态
            param.checkStatusList = [1, 2, 3, 4]
            // 付款状态
            param.payStatusList = [0, 1]
          }
        }
      }
    }
    if (name == '运维费' || type == 7) {
      option = { ...routeAliasConfig['maintenanceCost'] }
      // 点击的是一级的金额
      if (!childName) {
        // 费用状态
        param.feeStatuses = [0, 1, 5]
      } else {
        if (childName == '保险费' || type == 8) {
          option = { ...routeAliasConfig['carInsure'] }
        } else {
          // 费用状态
          if (isPaid) {
            param.feeStatuses = [1]
          } else if (isWaitPaid) {
            param.feeStatuses = [0, 5]
          } else {
            param.feeStatuses = [0, 1, 5]
          }
          // 费用名称
          if (childName == '维修费' || type == 10) {
            param.feeNameIdList = [53]
          } else if (childName == '保养费' || type == 9) {
            param.feeNameIdList = [52]
          } else if (childName == '年检费' || type == 11) {
            param.feeNameIdList = [54]
          }
        }
      }
    }
    if (['车辆购置费', '购车其他费'].includes(name) || [4, 5].includes(type)) {
      option = { ...routeAliasConfig['carPurchase'] }
      param.operateStatusList = [10, 20, 30, 60]
      if (isPaid) {
        param.feeStatuses = [1, 2]
      } else if (isWaitPaid) {
        param.feeStatuses = [0, 3, 5]
      } else {
        param.feeStatuses = [0, 1, 2, 3, 5]
      }
      if (name == '车辆购置费' || type == 4) {
        param.feeNameIdList = [51, 57]
      }
      if (name == '购车其他费' || type == 5) {
        param.feeNameIdList = [62, 63, 64, 65, 66]
      }
    }
  }

  return {
    ...option,
    path: option.path + setQueryParams(param),
  }
}

/**
 * 根据给定的代码和额外数据导航到有相应权限的页面
 * 此函数首先获取导航配置，然后检查用户是否具有访问该页面的权限
 * 如果用户有权限，则导航到指定页面；如果没有权限，则显示警告消息
 *
 * @param code - 用于获取导航配置的代码
 * @param extraData - 额外数据项
 * @param queryParam - 查询条件参数
 */
const nav2WithCode = (code: string, extraData: any, queryParam: any, redirectTo: Function) => {
  // 获取导航配置
  const config = getNavConfig(code, extraData, queryParam)

  // 检查用户是否具有访问权限
  const hasPermission = usePermission(config.permission)

  if (hasPermission) {
    // 具有权限，则导航到指定页面
    redirectTo(config.path, config.label)
  } else {
    // 没有权限，则显示警告消息
    Message.warning(`您没有访问该页面的权限`)
  }
}

export { nav2WithCode }
